const db = require('../db/index.js');
// 导入加密中间件
const bcrypt = require('bcryptjs');
// 导入nodejs的crypto生成onlyid
const crypto = require('crypto');
// 处理文件路径
fs = require('fs');

// 创建订单
exports.createOrder = (req, res) => {
    const { id, goods_id } = req.body;
    console.log(req.body, 777);
    // 初始化订单状态为 "未发货"
    const status = '未发货';
    const sql = 'INSERT INTO orders (id, goods_id, status) VALUES (?, ?, ?)';
    db.query(sql, [id, goods_id, status], (err, result) => {
        if (err) return res.cc(err);
        res.send({
            status: 0,
            message: '订单创建成功',
        });
    });
};


// 获取订单
exports.getOrder = (req, res) => {
	const sql = 'select * from orders where id = ?'
	db.query(sql, req.body.id, (err, result) => {
		if (err) return res.cc(err)
		res.send(result)
	})
}


// 获取全部订单
exports.getOrderAll = (req, res) => {
	const sql = 'select * from orders'
	db.query(sql, (err, result) => {
		if (err) return res.cc(err)
		res.send(result)
	})
}


// 发货
exports.deliveryOrder = (req, res) => {
    const {order_id} = req.body
	console.log(req.body,9999);
    const sql = 'UPDATE orders SET status = ? WHERE order_id = ?'; 
    const status = '已发货'; 
    db.query(sql, [status, order_id], (err, result) => {
        if (err) return res.cc(err);
        res.send({
            status: 0,
            message: '订单已成功发货', 
        });
    });
};


// 查找订单号
exports.findOrderId = (req, res) => {
    const { id, goods_id } = req.body;
    const sql = 'SELECT order_id FROM orders WHERE id = ? AND goods_id = ?';
    db.query(sql, [id, goods_id], (err, results) => {
        if (err) return res.cc(err);
        res.send({
            message: '订单查找成功',
            data: results
        });
    });
};


